package org.jeecg.modules.polaris.job.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.polaris.job.entity.Job;
import org.jeecg.modules.polaris.job.service.IJobService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: job
 * @Author: jeecg-boot
 * @Date:   2020-10-03
 * @Version: V1.0
 */
@Api(tags="job")
@RestController
@RequestMapping("/polaris/job")
@Slf4j
public class JobController extends JeecgController<Job, IJobService> {
	@Autowired
	private IJobService jobService;

	 /**
	  *
	  * @param college
	  * @param pageNo
	  * @param pageSize
	  * @return
	  */
	@AutoLog(value = "job-分页列表查询")
	@ApiOperation(value="job-分页列表查询", notes="job-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(@RequestParam(name="college", required=true) String college,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
//		QueryWrapper<Job> queryWrapper = QueryGenerator.initQueryWrapper(job, req.getParameterMap());

		LambdaQueryWrapper<Job> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(Job::getColleage,college)
				.eq(Job::getEmplStatus,"协议就业")
				.groupBy(Job::getRegSignDept);
		Page<Job> page = new Page<Job>(pageNo, pageSize);
		IPage<Job> pageList = jobService.page(page, queryWrapper);
		return Result.OK(pageList);

	}
	
	/**
	 *   添加
	 *
	 * @param job
	 * @return
	 */
	@AutoLog(value = "job-添加")
	@ApiOperation(value="job-添加", notes="job-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Job job) {
		jobService.save(job);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param job
	 * @return
	 */
	@AutoLog(value = "job-编辑")
	@ApiOperation(value="job-编辑", notes="job-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Job job) {
		jobService.updateById(job);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "job-通过id删除")
	@ApiOperation(value="job-通过id删除", notes="job-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		jobService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "job-批量删除")
	@ApiOperation(value="job-批量删除", notes="job-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.jobService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "job-通过id查询")
	@ApiOperation(value="job-通过id查询", notes="job-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Job job = jobService.getById(id);
		if(job==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(job);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param job
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Job job) {
        return super.exportXls(request, job, Job.class, "job");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Job.class);
    }
	 /**
	  *
	  * @param college
	  * @return
	  */
	 @AutoLog(value = "查询公司列表")
	 @ApiOperation(value="查询公司列表", notes="通过学院-查询company")
	 @PostMapping(value = "/getCompanyList")
	 public Result<?> getCompany(@RequestParam(name="college", required=true) String college,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		 LambdaQueryWrapper<Job> queryWrapper = new LambdaQueryWrapper<>();
		 queryWrapper.eq(Job::getColleage,college)
				 .eq(Job::getEmplStatus,"协议就业")
				 .groupBy(Job::getRegSignDept);

		 Page<Job> page = new Page<Job>(pageNo, pageSize);
		 IPage<Job> pageList = jobService.page(page, queryWrapper);

		 if(pageList.getRecords().size()==0){
			 return Result.error("未找到数据");
		 }
		 return Result.OK(pageList);
	 }


	 /**
	  *
	  * @param college
	  * @param company
	  * @return
	  */
	 @AutoLog(value = "查询公司入职人员")
	 @ApiOperation(value="查询公司入职人员", notes="job-查询此company入职人员")
	 @PostMapping(value = "/getStudentList")
	 public Result<?> queryPageList(@RequestParam(name="college", required=true) String college,
									@RequestParam(name="company", required=true) String company,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		 QueryWrapper<Job> query = new QueryWrapper<>();
		 query.eq("reg_sign_dept",company);
		 query.eq("colleage",college);
		 query.eq("empl_status","协议就业");
		 Page<Job> page = new Page<Job>(pageNo, pageSize);
		 IPage<Job> pageList = jobService.page(page, query);
		 if(pageList == null){
		 	return Result.error("查询失败");
		 }
		 if(pageList.getRecords().size()==0){
			 return Result.error("未找到数据");
		 }
		 return Result.OK(pageList);
	 }


	 /**
	  *
	  * @param search
	  * @return
	  */
	 @AutoLog(value = "搜索列表")
	 @ApiOperation(value="搜索列表", notes="通过公司名称进行搜索")
	 @PostMapping(value = "/getSearchList")
	 public Result<?> getSearchList(@RequestParam(name="search", required=true) String search,
									@RequestParam(name="college", required=true) String college,
									@RequestParam(name="gradenum", required=true) String gradenum){
//		 QueryWrapper<Job> query = new QueryWrapper<>();
//		 query.eq("reg_sign_dept",company);
//		 query.eq("colleage",college);
//		 List<Job> list = jobService.list(query);
//		 if(list == null){
//			 return Result.error("没有找到入职此公司的人员");
//		 }
		 LambdaQueryWrapper<Job> queryWrapper = new LambdaQueryWrapper<>();
		 queryWrapper.eq(Job::getColleage,college)
				     .eq(Job::getGradenum,gradenum)
				     .eq(Job::getEmplStatus,"协议就业")
				      .like(Job::getRegSignDept,search)
				      .groupBy(Job::getRegSignDept); ;
		 List<Job> list = jobService.list(queryWrapper);
		 if(list == null){
		 	return Result.error("无此公司");
		 }
		 return Result.OK(list);
	 }
}


